package com.shengqi.xinnongren.work.ui.user.activities;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.shengqi.xinnongren.R;
import com.shengqi.xinnongren.common.base.BaseMvpActivity;
import com.shengqi.xinnongren.common.md5.SafeUtils;
import com.shengqi.xinnongren.common.utils.LogUtil;
import com.shengqi.xinnongren.common.utils.ToastHelper;
import com.shengqi.xinnongren.work.model.HttpRespond;
import com.shengqi.xinnongren.work.ui.user.model.MessageListBean;
import com.shengqi.xinnongren.work.ui.user.presenter.MessageListPresenter;
import com.shengqi.xinnongren.work.ui.user.view.MessageListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MessageListActivity extends BaseMvpActivity<MessageListView, MessageListPresenter> implements MessageListView {

    public static final String INTENT_TYPE = "TYPE";

    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;

    private String type;
    private int pageIndex = 0;

    private List<MessageListBean> mList;
    private MessageListAdapter mAdapter;

    public static void start(Context context, String type) {
        Intent intent = new Intent(context, MessageListActivity.class);
        intent.putExtra(INTENT_TYPE, type);
        context.startActivity(intent);
    }

    @Override
    protected int setContentLayoutRes() {
        return R.layout.activity_message_list;
    }

    @Override
    protected void initView() {

        Intent intent = getIntent();
        if (intent != null) {
            type = intent.getStringExtra(INTENT_TYPE);
        }

        mList = new ArrayList<>();
        mAdapter = new MessageListAdapter(R.layout.item_msg, mList);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    protected void initData() {
        mPresenter.getMessageList(type, pageIndex);
        mRefreshLayout.setOnRefreshListener(refreshLayout -> refreshData()).setOnLoadMoreListener(refreshLayout -> mPresenter.getMessageList(type, pageIndex));
    }

    private void refreshData() {
        mList.clear();
        pageIndex = 0;
        mPresenter.getMessageList(type, pageIndex);
    }

    @Override
    protected MessageListPresenter createPresenter() {
        return new MessageListPresenter();
    }

    @Override
    public void showLoadingView() {
        showLoadingDialog();
    }

    @Override
    public void hideLoadingView() {
        hideLoadingDialog();
    }

    @Override
    public void onNetworkError() {
        hideLoadingView();
        mRefreshLayout.finishRefresh().finishLoadMore();
    }

    @Override
    public void showMessageList(List<MessageListBean> list) {
        mRefreshLayout.finishRefresh().finishLoadMore();

        if (list.size() > 0) {
            pageIndex++;
        }
        mList.addAll(list);
        mAdapter.notifyDataSetChanged();
    }


    class MessageListAdapter extends BaseQuickAdapter<MessageListBean, BaseViewHolder> {

        public MessageListAdapter(int layoutResId, @Nullable List<MessageListBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, MessageListBean item) {
            helper.setText(R.id.tv_time, item.getSendTime());
            helper.setText(R.id.tv_title, item.getTitle());
            helper.setText(R.id.tv_content, item.getContent());
        }
    }
}
